import { defineStore } from 'pinia';

export const useGetMenu = defineStore('myMenu',{
    state:()=>({
        isCollapse:false,
        routerMap:{
            '/home':[
                {
                    path:'/home',
                    label:'首页',
                },
            ],
            '/shop':[
                {
                    path:'/shop',
                    label:'商品管理'
                }
            ],
            '/user':[
                {
                    path:'/user',
                    label:'用户管理'
                }
            ],
            '/other/other1':[
                {
                    path:'',
                    label:'其他',
                },
                {
                    path:'/other/other1',
                    label:'其他1'
                }
            ],
            '/other/other2':[
                {
                    path:'',
                    label:'其他',
                },
                {
                    path:'/other/other2',
                    label:'其他2'
                }
            ],
            '/other/other3':[
                {
                    path:'',
                    label:'其他',
                },
                {
                    path:'/other/other3',
                    label:'其他3'
                }
            ],
            '/other/other4':[
                {
                    path:'',
                    label:'其他',
                },
                {
                    path:'/other/other4',
                    label:'其他4'
                }
            ]
        },
        activeUrl:'/home',
        activeUrlArr:[
            {
                path:'/home',
                label:'首页'
            }
        ],
    }),
    getters:{},
    actions: {
        updataCollapse(){
            this.isCollapse = !this.isCollapse;
        },
        updataActiveUrlArr(path){
            let arrObj = Object.assign(this.activeUrlArr);
            arrObj.unshift(...this.routerMap[path]);
            arrObj = arrObj.filter(item=>item?.path);
            let map = new Map();
            for (let item of arrObj) {
                if (!map.has(item.path)) {
                    map.set(item.path, item);
                };
            };
            const arr = [...map.values()];
            this.activeUrlArr = arr;
        },
        removeActiveUrlArr(tag){
            this.activeUrlArr = this.activeUrlArr.filter((item)=> item.path !== tag);
            let currentActiveUrl = '';
            if(this.activeUrlArr.length > 0 && this.activeUrl === tag ){
                currentActiveUrl = this.activeUrlArr[0].path;
                this.activeUrl = currentActiveUrl;
            }
        }
    },
    persist: true,
});